package etcd

import (
	"fmt"
	"google.golang.org/grpc"
	"net"
)

type (
	// RegisterFn defines the method to register a server.
	RegisterFn func(*grpc.Server)
)

func NewServer(address string, register RegisterFn) (*grpc.Server, error) {
	lis, err := net.Listen("tcp", address)
	if err != nil {
		return nil, err
	}

	server := grpc.NewServer()
	register(server)

	go func() {
		if err := server.Serve(lis); err != nil {
			panic(err)
		}
	}()

	fmt.Printf("service started listen on: %s\n", address)
	return server, nil
}